package cn.virens.web.common;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import cn.hutool.core.util.ArrayUtil;
import cn.virens.common.RequestUtil;
import cn.virens.components.page.PageParamUser;
import cn.virens.database.mysql.model.sys.SysUser;
import cn.virens.web.components.shiro.comm.SessionUtil;

/**
 * 通用控制器-后台
 * 
 * @author virens
 */
public class BaseUserController extends BaseJSONController {
	protected String _identity;

	protected BaseUserController() {
		super();
	}

	/**
	 * @param baseTemplateUrl 模板根路径
	 */
	protected BaseUserController(String baseTemplateUrl) {
		super(baseTemplateUrl);
	}

	/**
	 * @param baseTemplateUrl 模板根路径
	 * @param identity        顶层标示
	 */
	protected BaseUserController(String baseTemplateUrl, String identity) {
		super(baseTemplateUrl);
		this._identity = identity;
	}

	/**
	 * 获取 当前登录的用户
	 * 
	 * @param  request
	 * @return
	 */
	protected final SysUser getUser(HttpServletRequest request) {
		return useSession(request, (session) -> SessionUtil.getUserInfo(session));
	}

	/**
	 * 获取登录用户的唯一识别码
	 * 
	 * @param  request
	 * @return
	 */
	protected final String getUserUuid(HttpServletRequest request) {
		return useSession(request, (session) -> SessionUtil.getUserUuid(session));
	}

	/**
	 * 获取登录用户的用户名
	 * 
	 * @param  request
	 * @return
	 */
	protected final String getUserNume(HttpServletRequest request) {
		return useSession(request, (session) -> SessionUtil.getUserNume(session));
	}

	/**
	 * 获取登录用户的角色标识
	 * 
	 * @param  request
	 * @return
	 */
	protected final String getRoleKeys(HttpServletRequest request) {
		return useSession(request, (session) -> SessionUtil.getRoleKeys(session));
	}

	/**
	 * 获取登录用户的角色ID
	 * 
	 * @param  request
	 * @return
	 */
	protected final String getGroupKeys(HttpServletRequest request) {
		return useSession(request, (session) -> SessionUtil.getGroupKeys(session));
	}

	/**
	 * 获取Subject
	 * 
	 * @param  request HttpServletRequest
	 * @return         Subject
	 */
	protected final Subject getSubject(HttpServletRequest request) {
		return SecurityUtils.getSubject();
	}

	/**
	 * 检查角色
	 * 
	 * @param request         HttpServletRequest
	 * @param roleIdentifiers 权限列表
	 */
	protected final void checkRoles(HttpServletRequest request, String... roleIdentifiers) {
		getSubject(request).checkRoles(roleIdentifiers);
	}

	protected final boolean hasRoles(HttpServletRequest request, String... roleIdentifiers) {
		return ArrayUtil.contains(getSubject(request).hasRoles(Arrays.asList(roleIdentifiers)), true);
	}

	/**
	 * 检查权限
	 * 
	 * @param request    HttpServletRequest
	 * @param permission 权限列表
	 */
	protected final void checkPermission(HttpServletRequest request, String... permission) {
		getSubject(request).checkPermissions(permission);
	}

	protected final boolean hasPermission(HttpServletRequest request, String... permissions) {
		return ArrayUtil.contains(getSubject(request).isPermitted(permissions), true);
	}

	protected PageParamUser param(HttpServletRequest request) {
		Integer pageSize = RequestUtil.getValueInt(request, "limit", 10);
		Integer pageNum = RequestUtil.getValueInt(request, "page", 1);

		PageParamUser pageParam = new PageParamUser();
		pageParam.setAccount(getUserUuid(request));
		pageParam.setGroup(getGroupKeys(request));
		pageParam.setRole(getRoleKeys(request));
		pageParam.setPageSize(pageSize);
		pageParam.setPageNum(pageNum);

		return pageParam;
	}
}
